package com.ecb.business.service.impl;

import com.ecb.business.constant.AuditConstant;
import com.ecb.business.dto.evidence.EvidenceDTO;
import com.ecb.business.dto.evidence.QueryEvidenceDTO;
import com.ecb.business.exception.BusinessException;
import com.ecb.business.po.ChainEvidencePO;
import com.ecb.business.repository.ChainEvidenceDao;
import com.ecb.business.service.AuditService;
import com.ecb.business.service.EvidenceManageService;
import com.ecb.business.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Optional;

@Service
public class EvidenceManageServiceImpl implements EvidenceManageService {
    @Autowired
    ChainEvidenceDao chainEvidenceDao;
    @Autowired
    AuditService auditService;

    @Override
    @Transactional
    public boolean addEvidence(EvidenceDTO dto) {
        ChainEvidencePO chainEvidencePO = JsonUtils.beanToBean(dto, ChainEvidencePO.class);
        chainEvidenceDao.save(chainEvidencePO);
        // 添加审计
        auditService.addAudit(AuditConstant.OPERATE_TYPE_ADD_EVIDENCE, AuditConstant.OPERATE_TARGET_EVIDENCE, chainEvidencePO.getCreateUser(), null, JsonUtils.beanToJson(chainEvidencePO));
        return true;
    }


    @Override
    public EvidenceDTO getAllEvidenceByRole(QueryEvidenceDTO queryDTO) {
        ChainEvidencePO res = chainEvidenceDao.findAllByEvidenceRoleAndStatusEquals(queryDTO.getEvidenceRole(), 1);
        EvidenceDTO evidenceDTO = JsonUtils.beanToBean(res, EvidenceDTO.class);
        return evidenceDTO;
    }

    @Override
    public EvidenceDTO getEvidenceById(QueryEvidenceDTO queryEvidenceDTO) {
        Optional<ChainEvidencePO> byId = chainEvidenceDao.findById(queryEvidenceDTO.getId());
        if (!byId.isPresent()){
            throw new BusinessException("500","证书不存在");
        }
        EvidenceDTO evidenceDTO = JsonUtils.beanToBean(byId.get(), EvidenceDTO.class);
        return evidenceDTO;
    }

    @Override
    @Transactional
    public boolean updateEvidence(EvidenceDTO evidenceDTO) {
        ChainEvidencePO chainEvidencePO = JsonUtils.beanToBean(evidenceDTO, ChainEvidencePO.class);
        Optional<ChainEvidencePO> byId = chainEvidenceDao.findById(evidenceDTO.getId());
        if (!byId.isPresent()){
            throw new BusinessException("500","证书不存在");
        }
        chainEvidencePO.setModifyTime(new Date());
        chainEvidenceDao.save(chainEvidencePO);
        auditService.addAudit(AuditConstant.OPERATE_TYPE_UPDATE_EVIDENCE, AuditConstant.OPERATE_TARGET_EVIDENCE, chainEvidencePO.getModifyUser(), JsonUtils.beanToJson(byId.get()), JsonUtils.beanToJson(chainEvidencePO));
        return true;
    }

    @Override
    public boolean deleteEvidence(EvidenceDTO evidenceDTO) {
        evidenceDTO.setStatus(9);
        // 记录审计
        auditService.addAudit(AuditConstant.OPERATE_TYPE_DELETE_EVIDENCE, AuditConstant.OPERATE_TARGET_EVIDENCE, evidenceDTO.getModifyUser(), JsonUtils.beanToJson(evidenceDTO), null);
        return updateEvidence(evidenceDTO);
    }
}
